package com.lingxi.work;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LeapYearChecker {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JLabel yearLabel = new JLabel("请输入一个年份的整数：");
        JTextField yearField = new JTextField(4);
        JButton checkButton = new JButton("检验按钮");
        checkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int year = Integer.parseInt(yearField.getText());
                    boolean isLeap = isLeapYear(year);
                    JOptionPane.showMessageDialog(frame, (isLeap ? " 输入的年份是闰年。" : " 输入的年份不是闰年。"));
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(frame, "请输入有效的整数年份。", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        frame.add(yearLabel);
        frame.add(yearField);
        frame.add(checkButton);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = 300, height = 100;
        frame.setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
        frame.setVisible(true);
    }
    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}
